package handler

import (
	"article/api/request"
	"article/inter/dao"
	"article/inter/model"
	"github.com/gin-gonic/gin"
	"net/http"
)

func CreateCom(c *gin.Context) {
	var req request.CreateCom
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	var com model.Comments
	dao.GetOneByFields(&model.Comments{
		ArticleId: uint32(req.ArticleId),
	}, &com)
	if com.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "此篇文章不存在",
			"data": nil,
		})
		return
	}
	com = model.Comments{
		ArticleId: uint32(req.ArticleId),
		Common:    req.Common,
		Like:      uint32(req.Like),
		Num:       uint32(req.Num),
		Reply:     req.Reply, //回复
	}
	if !dao.Create(&com) {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "文章评论失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "文章评论成功",
		"data": com,
	})
}

func FindCom(c *gin.Context) {
	var com []model.Comments
	dao.GetListByFields(&model.Comments{}, &com)

	var coms []*model.Comments
	for _, v := range com {
		coms = append(coms, &v)
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": coms,
	})
}

func DelCom(c *gin.Context) {
	var req request.DelCom
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": dao.Delete(&model.Comments{Id: uint64(req.Id)}),
	})

}
